﻿Ext.namespace('sys');
Ext.onReady(main);

function main() {
	Ext.BLANK_IMAGE_URL = 'extjs/resources/images/default/s.gif';
	Ext.chart.Chart.CHART_URL = 'extjs/resources/charts.swf';
	Ext.QuickTips.init();
	Ext.Ajax.disableCaching = true;
	Ext.grid.GridPanel.prototype.trackMouseOver = !Ext.isIE;
	Ext.form.TextArea.prototype.style = 'word-wrap:normal;overflow-x:auto;overflow-y:auto';
	var urlStr = window.location.href;
	var dsStr = 'datastruct';
	urlStr = urlStr.toLowerCase();
	dsStr = dsStr.toLowerCase();
	sys.dataStruct = false;
	for (var i = 0; i < urlStr.length - dsStr.length; ++i) {
		if (urlStr.slice(i, i + dsStr.length) == dsStr) {
			sys.dataStruct = true;
			break;
		}
	}
	getSystemSettings(function() {
				getCurrentUserInfo(onloaded)
			})

	function onloaded() {
		showMainPage();
		removeLoading();
	}
}

function getCurrentUserInfo(handler) {
	ajax({
				url : url.getCurrentUserInfo,
				nomsg : true,
				success : onuser,
				failure : onguest
			});
	function onuser(obj) {
		sys.user = obj;
		sys.god = obj.god;
		sys.teacher = obj.teacher;
		sys.guest = false;
		sys.student = !Ext.isEmpty(sys.user.sid);
		sys.newmsg = 0;
		sys.canSendMsg = sys.god || sys.student || sys.settings.openOutsideSendMsg;
		sys.canViewDiscuss = sys.god || sys.settings.openDiscuss;
		sys.canDiscuss = sys.canViewDiscuss && (sys.student || sys.settings.openOutsideDiscuss);

		if (!sys.debug) {
			if (sys.newmsgtask) sys.newmsgtask.stopAll();
			sys.newmsgtask = new Ext.util.TaskRunner();
			sys.newmsgtask.start({
						run : inquiryNewMsg,
						interval : 10000
					});
		}

		handler();
	}
	function onguest() {
		sys.user = null;
		sys.god = false;
		sys.guest = true;
		sys.student = false;
		sys.newmsg = 0;
		sys.canSendMsg = false;
		sys.canViewDiscuss = false;
		sys.canDiscuss = false;

		if (sys.newmsgtask) sys.newmsgtask.stopAll();
		sys.newmsgtask = null;

		handler();
	}
}

function getSystemSettings(handler) {
	ajax({
				url : url.getSystemSettings,
				handler : onsuccess
			});
	function onsuccess(obj) {
		sys.settings = obj;
		handler();
	}
}

function removeLoading() {
	Ext.get('loading').remove();
	Ext.get('loading-mask').fadeOut({
				remove : true
			});
}
